package com.teachoa.bean;

import org.snaker.jfinal.plugin.SnakerPlugin;
import org.snaker.engine.entity.Process;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.teachoa.constant.Global;
import com.teachoa.enumtype.ProjectStatusEnum;
import com.teachoa.enumtype.UserTypeEnum;

public class Project extends Model<Project> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2210130805930734813L;
	
	public static final Project dao = new Project();
	
	public Page<Project> findTeacherProjectsByPage(int pageNo, String teacherId) {
		return dao.paginate(pageNo, Global.pageSize, "select a.*"
				, " from project a join uncertain b on a.uncertain_id = b.id where b.user_id = ?"
						+ " order by a.create_time desc", teacherId);
	}
	
	public void audit(String projectId, int isPassed, String errorInfo) {
		Project project = new Project();
		project.set("id", projectId);
		int status = 0;
		if(isPassed==0) {
			status = ProjectStatusEnum.PRO.getCode();
			project.set("error_info", errorInfo);
		} else if(isPassed==1)
			status = ProjectStatusEnum.PROSUCCESS.getCode();
		project.set("status", status);
		project.update();
	}
	
	public Page<Project> findStudentProjectsByPage(int pageNo, String studentId) {
		return dao.paginate(pageNo, Global.pageSize, "select *", "from project where user_id = ?"
				+ " order by create_time desc", studentId);
	}
	
	public String getFlowIdByProjectId(String projectId) {
		return Db.queryStr("select b.flow_id from project a join uncertain b on a.uncertain_id = b.id"
				+ " where a.id = ?", projectId);
	}
	
	public void updateStatus(String projectId, int status) {
		Project project = new Project();
		project.set("id", projectId);
		project.set("status", status);
		project.update();
	}
	
	public Project getProjectByOrderId(String orderId) {
		return Project.dao.findFirst("select * from project where order_id = ?", orderId);
	}
	
	public Page<Project> findProjectsByPage(int pageNo, int userType, String userId) {
		if(userType==UserTypeEnum.STUDENT.getCode())
			return findStudentProjectsByPage(pageNo, userId);
		else if(userType==UserTypeEnum.TEACHER.getCode())
			return findTeacherProjectsByPage(pageNo, userId);
		return new Page<Project>(null, 1, Global.pageSize, 1, 0);
	}
	
	public Project findProjectDetail(String id) {
		Project project = dao.findById(id);
		String uncertainId = project.getStr("uncertain_id");
		Uncertain uncertain = Uncertain.dao.findById(uncertainId);
		String processId = uncertain.getStr("flow_id");
		
		Process process = SnakerPlugin.getEngine().process().getProcessById(processId);
		uncertain.put("process", process);
		project.put("uncertain", uncertain);
		
		return project;
	}

	public Page<Project> findAdminProjectsByPage(int pageNo) {
		return dao.paginate(pageNo, Global.pageSize, "select *", "from project");
	}
	
	public boolean findProjectByName(String name) {
		String sql = "select count(*) as num from project where name = ?";
		Record record = Db.findFirst(sql, name);
		int num = record.getLong("num").intValue();
		return num==0;
	}

	public Project findProjectByOrderId(String orderId) {
		String sql = "select * from project where order_id = ?";
		return Project.dao.findFirst(sql, orderId);
	}
	
}
